<?php

namespace app\api\command;

use app\common\model\Packages;
use app\common\model\User;
use app\common\model\UserCommission;
use app\common\model\UserPackagesConfig;
use fast\Random;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class OrderSettlementdaili extends Command
{
    protected function configure()
    {
        $this->setName('order_settlement_daili')->setHelp('用户端待结算订单结算')->setDescription('用户端待结算订单结算');

    }

    protected function execute(Input $input, Output $output)
    {
        $orderModel = new \app\common\model\Order();
        //订单状态:0=已取消,1=待充值,2=待收货,3=待处理,4=待结算,5=已结算,6=话费报销
        $where['platform_type'] = 4;
        $where['is_agency']     = 2;
        $order                  = $orderModel->where($where)->whereIn('order_status',
            '1,2,3,4')->order('id desc')->select();
//        $order      = $orderModel->where(['id'=>295])->order('id desc')->select();
        if ($order) {
            $time          = time();
            $packagesModel = new \app\admin\model\Packages();
            foreach ($order as $k => $v) {
                sleep(2);
                $packageInfo = $packagesModel->where(['id' => $v['goods_id']])->find();
                $this->orderInfo172($v, $packageInfo, $time);
            }
        }
    }

    public function orderInfo172($orderInfo, $packageInfo, $currentTimestamp)
    {
        $url                 = config('site.url_172');
        $key                 = config('site.key_172');
        $user_id             = config('site.zhanghao_172');
        $data['user_id']     = $user_id;
        $data['Timestamp']   = time();
        $data['DownOrderID'] = $orderInfo['order_id'];
        $string              = ascll172($data);
        $data['user_sign']   = strtolower(md5($string.$key));
        $url                 = $url.'/api/order/GetOrderInfo';
        $res                 = httpPost($url, $data);
        $arr                 = json_decode($res, true);
//        $arr['data']['orderNo'] = '23';
//        $arr['data']['orderStatus'] = '已结算';
//        $arr['data']['remark'] = '测试';
        if (!empty($arr['data']['orderNo'])) {
            if (!empty($arr['data']['expressName'])) {
                $orderInfo->shipping_company_name = $arr['data']['expressName'];
            }
            if (!empty($arr['data']['expressCode'])) {
                $orderInfo->shipping_no = $arr['data']['expressCode'];
            }
            if (!empty($arr['data']['thirdPhone'])) {
                $orderInfo->phone = $arr['data']['thirdPhone'];
            }
            if ($orderInfo['is_verification'] == 0) {
                $yuqi_hexiao = 3;
                $yes_hexiao = 2;
            } else {
                $yuqi_hexiao = 6;
                $yes_hexiao = 5;
            }
            if (time() > $orderInfo->hexiao_next_time && $orderInfo->hexiao_next_time > 0) {
                $orderInfo->is_hexiao = $yuqi_hexiao;
            }else{
                $orderInfo->is_hexiao = $yes_hexiao;
            }
            if ($arr['data']['orderStatus'] == '已结算') {
                if ($arr['data']['remark']) {
                    $orderInfo->remark = $arr['data']['remark'].'+'.$arr['data']['orderStatus'];
                    $orderInfo->is_tripartite_settlement = 1;
//                    if ($orderInfo->audit_status != 2) {
//                        $orderInfo->order_status             = 4;
//                    } else {
                        $userModel                  = new User();
                        $orderInfo->is_hexiao       = 2;
                        $orderInfo->order_status    = 5;
                        $orderInfo->settlement_time = time();
                        if ($orderInfo->is_more == 2) {
                            $orderInfo->two_js_time = settlement_time($orderInfo->settlement_time);//二次结算时间
                            $huafei                 = $packageInfo->bxhf_text;
                            $orderInfo->more_num    = 1;
                            if ($packageInfo->day != 6) {
                                $orderInfo->hexiao_next_time = 7 * 86400 + time();
                            } else {
                                $orderInfo->hexiao_next_time = $orderInfo->two_js_time;
                            }
                        } else {
                            //报销话费
                            $huafei    = bcadd($packageInfo->ciyue_set_meal, $packageInfo->bxhf_text);
                        }
                        if ($huafei) {
                            $userModel::money($huafei, $orderInfo->user_id, '话费报销', $orderInfo->id,
                                $packageInfo['id']);
                            $orderInfo->baoxiao_time   = time();
                            $orderInfo->baoxiao_status = 2;
                        }
                        $v                     = $orderInfo;
//                        $userInfo              = $userModel->where(['id' => $v['user_id']])->find();
//                        $zigou_commission      = $packageInfo['one_commission'] ?? 0;
//                        $one_commission        = $packageInfo['junior_contribution'] ?? 0;
//                        $tjyj                  = $packageInfo['junior_contribution_tow'] ?? 0;
//                        $pplots_profit         = $packageInfo['pplots_profit'] ?? 0;        //区县
//                        $pplots_yunying_profit = $packageInfo['pplots_yunying_profit'] ?? 0;//小区
                        jiangli($v, $packageInfo);
                        if ($v['parent_id']) {
                            jiangli($v, $packageInfo, 2);
                        }
                        if ($v['two_id']) {
                            jiangli($v, $packageInfo, 3);
                        }
                        //区县
                        if ($v['agency_area_id']) {
                            jiangli($v, $packageInfo, 4);
                        }
                        //小区
                        if ($v['plot_id']) {
                            jiangli($v, $packageInfo, 5);
                        }
//                        if ($packageInfo['duoci'] || $v->is_more == 2) {
//                            jiangli($v, $packageInfo);
//                            if ($v['parent_id']) {
//                                jiangli($v, $packageInfo, 2);
//                            }
//                            if ($v['two_id']) {
//                                jiangli($v, $packageInfo, 3);
//                            }
//                            //区县
//                            if ($v['agency_area_id']) {
//                                jiangli($v, $packageInfo, 4);
//                            }
//                            //小区
//                            if ($v['plot_id']) {
//                                jiangli($v, $packageInfo, 5);
//                            }
//                        } else {
//                            if ($userInfo) {
//                                if ($zigou_commission) {
//                                    $userModel::money($zigou_commission, $v['user_id'], '自购', $v['id'],
//                                        $packageInfo['id']);
//                                }
//                                if ($one_commission) {
//                                    $userModel::money($one_commission, $v['parent_id'], '直推奖励', $v['id'],
//                                        $packageInfo['id']);
//                                }
//                                if ($userInfo['two_id'] && $tjyj) {
//                                    $userModel::money($tjyj, $userInfo['two_id'], '下下级贡献', $v['id'],
//                                        $packageInfo['id']);
//                                }
//                            }
//                            if ($v['agency_area_id'] && $pplots_profit) {
////                                $agencyInfo = \app\admin\model\user\Agency::where(['user_id'=>$v['agency_area_id']])->find();
////                                if($agencyInfo['money'] > $pplots_profit){
////                                    if($agencyInfo->money > $pplots_profit){
////                                        $agencyInfo->money -= $pplots_profit;
////                                        $agencyInfo->save();
////                                        $pplots_profit = 0;
////                                    }else{
////                                        $pplots_profit = $pplots_profit - $agencyInfo->money;
////                                        $agencyInfo->money = 0;
////                                        $agencyInfo->save();
////                                    }
////                                }
////                                if($pplots_profit){
////                                    $userModel::money($pplots_profit, $v['agency_area_id'], '区县运营分润', $v['id'],
////                                        $packageInfo['id']);
////                                }
//                                $userModel::money($pplots_profit, $v['agency_area_id'], '区县运营分润', $v['id'],
//                                    $packageInfo['id']);
//                            }
//                            if ($v['plot_id'] && $pplots_yunying_profit) {
////                                $agencyInfo = \app\admin\model\user\Agency::where(['user_id'=>$v['plot_id']])->find();
////                                if($agencyInfo['money'] > $pplots_yunying_profit){
////                                    if($agencyInfo->money > $pplots_yunying_profit){
////                                        $agencyInfo->money -= $pplots_yunying_profit;
////                                        $agencyInfo->save();
////                                        $pplots_yunying_profit = 0;
////                                    }else{
////                                        $pplots_yunying_profit = $pplots_yunying_profit - $agencyInfo->money;
////                                        $agencyInfo->money = 0;
////                                        $agencyInfo->save();
////                                    }
////                                }
////                                if($pplots_yunying_profit){
////                                    $userModel::money($pplots_yunying_profit, $v['plot_id'], '小区运营分润', $v['id'],
////                                        $packageInfo['id']);
////                                }
//                                $userModel::money($pplots_yunying_profit, $v['plot_id'], '小区运营分润', $v['id'],
//                                    $packageInfo['id']);
//                            }
//                        }
//                    }

                }
            } elseif ($arr['data']['orderStatus'] == '已发货') {
                $orderInfo->order_status = 2;
            } elseif ($arr['data']['orderStatus'] == '支付完成') {
                $orderInfo->order_status = 4;
            } elseif ($arr['data']['orderStatus'] == '待处理' || $arr['data']['orderStatus'] == '已提运营商') {
                $orderInfo->order_status = 3;
            } elseif ($arr['data']['orderStatus'] == "审核不通过" || $arr['data']['orderStatus'] == "已撤单" || $arr['data']['orderStatus'] == "已取消" || $arr['data']['orderStatus'] == "无法结算") {
                $orderInfo->order_status = 0;
            }
            $orderInfo->order_info_json = json_encode($arr['data'], true);
            $orderInfo->save();
            echo $arr['data']['orderStatus'];
        }
    }
}
